<?php

/**
 * @copyright : ienter asia Technologies
 * @author :  HungLV
 * @version    Release: V1.0
 * @Description : Class Base Controller
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MYAD_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
        $lang = $this->session->userdata('lang');
        if ($lang == "jp") {
            $this->lang->load('jp', 'japanese');
            $this->session->set_userdata('lang', 'jp');
            $this->config->set_item('language', 'japanese');
        }
        if ($lang == "en") {
            $this->lang->load('en', 'english');
            $this->session->set_userdata('lang', 'en');
            $this->config->set_item('language', 'english');
        }
        if ($lang == "") {
            $this->lang->load('jp', 'japanese');
            $this->session->set_userdata('lang', 'jp');
            $this->config->set_item('language', 'japanese');
        }
        if ($this->session->userdata('UserName')) {
            if ($this->session->userdata('Level')) {
                $level = $this->session->userdata('Level');
                if ($level == 2) {
                    switch ($this->router->fetch_class()) {
                        case 'mod_user':
                            redirect('/');
                            break;
                        case 'mod_conversion':
                            redirect('/');
                            break;
                        case 'mod_holiday':
                            redirect('/');
                            break;
                        case 'mod_leaveTypes':
                            redirect('/');
                            break;
                        case 'mod_salarypara':
                            redirect('/');
                            break;
                        case 'mod_pitpara':
                            redirect('/');
                            break;
                        case 'mod_shiftinfo':
                            redirect('/');
                            break;
                        case 'mod_salaryinfo':
                            redirect('/');
                            break;
                        case 'mod_pitinfo':
                            redirect('/');
                            break;
                        case 'mod_leaveinfo':
                            redirect('/');
                            break;
                        case 'mod_pitrules':
                            redirect('/');
                            break;
                        case 'mod_leavetypes':
                            redirect('/');
                            break;
                        case 'mod_employee':
                            redirect('/');
                            break;
                        case 'mod_employeebonus':
                            redirect('/');
                            break;
                        case 'mod_position':
                            redirect('/');
                            break;
                    }
                }
            } else {
                redirect('/');
            }
        } else {
            redirect('/');
        }
        if ($this->session->userdata('Level')) {
            //
        } else {
            redirect('/user');
        }
    }

    /**
     * Function check user login
     * check secssion user name
     */
    function checkExitLogin() {
        if ($this->session->userdata('UserName')) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Function pagination
     */
    function paginationData($total_data, $perpage, $url) {
        $this->load->library('pagination');
        //$perpage = 3;
        $config['total_rows'] = $total_data;
        $config['per_page'] = $perpage;
        $config['next_link'] = $this->lang->line('pagi_next');
        $config['prev_link'] = $this->lang->line('pagi_prev');
        $config['first_link'] = $this->lang->line('pagi_first');
        $config['last_link'] = $this->lang->line('pagi_last');
        $config['num_tag_open'] = '';
        $config['num_tag_close'] = '';
        $config['num_links'] = 4;
        $config['cur_tag_open'] = '<a class="h_pagination_active">';
        $config['cur_tag_close'] = '</a>';
        $config['base_url'] = base_url($url);
        // set curent
        $config['uri_segment'] = 2;
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }


    /**
     * @author Thanh NV        
     * @function validate_date 
     * @param: $incoming_date - format:YYYY/MM/DD         
     * Output: int
     * @This function check validate Date
     */
    public function validate_date($incoming_date = NULL) {
        /* Begin Quyet Upgrade 25/6/2014 */
        if (empty($incoming_date)) {
            return TRUE;
        } else {
            $rangeYear = $this->config->item('rangeYear');
            $dates = explode('/', $incoming_date);
            $month = !empty($dates[1]) ? $dates[1] : 0;
            $day = !empty($dates[2]) ? $dates[2] : 0;
            $year = !empty($dates[0]) ? $dates[0] : 0;
            if (count($dates) == 3 && $day >= 1 && $day <= 31 && $month >= 1 && $month <= 12 && $year >= $rangeYear['minYear'] && $year <= $rangeYear['maxYear'] && checkdate($month, $day, $year)) {
                return TRUE;
            } elseif (($year < $rangeYear['minYear'] || $year > $rangeYear['maxYear']) && checkdate($month, $day, $year) && count($dates) == 3) {
                $this->form_validation->set_message('validate_date', '%s ' . $this->lang->line('MC020'));
                return FALSE;
            } else {
                $this->form_validation->set_message('validate_date', '%s ' . $this->lang->line('MC010'));
                return FALSE;
            }
        }
        /* End Quyet Upgrade 25/6/2014 */
    }

    /**
     * @author Thanh NV        
     * @function validate_month 
     * @param: $incoming_date - format:YYYY/MM  or YYYY/M       
     * Output: int
     * @This function check validate Month
     */
    public function validate_month($incoming_date = NULL) {
        if (empty($incoming_date)) {
            return TRUE;
        } else {
            $rangeYear = $this->config->item('rangeYear');
            $dates = explode('/', $incoming_date);
            $month = !empty($dates[1]) ? $dates[1] : 0;
            $day = 01;
            $year = !empty($dates[0]) ? $dates[0] : 0;
            if (count($dates) == 2 && $day >= 1 && $day <= 31 && $month >= 1 && $month <= 12 && $year >= $rangeYear['minYear'] && $year <= $rangeYear['maxYear'] && checkdate($month, $day, $year)) {
                return TRUE;
            } elseif (($year < $rangeYear['minYear'] || $year > $rangeYear['maxYear']) && checkdate($month, $day, $year) && count($dates) == 2) {
                $this->form_validation->set_message('validate_month', '%s ' . $this->lang->line('MC020'));
                return FALSE;
            } else {
                $this->form_validation->set_message('validate_month', '%s ' . $this->lang->line('MC017'));
                return FALSE;
            }
        }
        /* End Quyet Upgrade 25/6/2014 */
    }

    /*
     * @author :        Quyet
     * @created:        23/6/2014
     * @Input :         (Interger) $id
     * @Description:    update or insert data into employee
     */

    function lessThanDate($endDateField = '', $startDateField = '') {
        if (empty($endDateField)) {
            return TRUE;
        } else {
            $endDate = strtotime($endDateField);
            $startDate = $this->input->post($startDateField);
            $startDate = strtotime($startDate);
            if ($endDate <= $startDate && !empty($startDateField)) {
                $this->form_validation->set_message('lessThanDate', '%s ' . $this->lang->line('emp_validate_date_lessthan') . ' ' . $this->input->post($startDateField));
                return FALSE;
            } else {
                return TRUE;
            }
        }
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Input :         (Interger) $id
     * @Description:    update or insert data into employee
     */

    function lessThanOrEqualDate($endDateField = '', $startDateField = '') {
        if (empty($endDateField)) {
            return TRUE;
        } else {
            $endDate = strtotime($endDateField);
            $startDate = $this->input->post($startDateField);
            $startDate = strtotime($startDate);
            if ($endDate < $startDate && !empty($startDateField)) {
                $this->form_validation->set_message('lessThanDate', '%s ' . $this->lang->line('emp_validate_date_lessthan') . ' ' . $this->input->post($startDateField));
                return FALSE;
            } else {
                return TRUE;
            }
        }
    }

    /*
     * @author :        Quyet
     * @created:        23/6/2014
     * @Input :         (Interger) $id
     * @Description:    is start month
     */

    function startMonth($dateFieldString = '') {
        if (empty($dateFieldString)) {
            return TRUE;
        } else {
            $dateFieldNumber = strtotime($dateFieldString);
            if (date('d', $dateFieldNumber) != 1) {
                $this->form_validation->set_message('startMonth', '%s ' . $this->lang->line('emp_validate_date_start_month'));
                return FALSE;
            } else {
                return TRUE;
            }
        }
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Input :         (string) $time
     * @Description:    validation time
     */

    public function validate_time($time = NULL) {
        if (empty($time)) {
            return TRUE;
        } else {
            $times = explode(':', $time);
            $hour = !empty($times[0]) ? $times[0] : FALSE;
            $minture = !empty($times[1]) ? $times[1] : FALSE;
            $flag = ctype_digit($hour) && ctype_digit($minture) && $hour >= 0 && $hour <= 23 && $minture >= 0 && $minture <= 59;
            if ((count($times) == 2) && $flag) {
                return TRUE;
            } else {
                $this->form_validation->set_message('validate_time', '%s ' . $this->lang->line('MC015'));
                return FALSE;
            }
        }
    }
    
    /**
        * @author Thanh NV        
        * @function validate_date 
        * @param: $float or $int
        * Output: TRUE or FALSE
        * @This function check validate float
        */
       public function validate_float($float = NULL) {     
               if(check_float_string($float)){ 
                   return TRUE;
               } else {
                   $this->form_validation->set_message('validate_float', '%s ' . $this->lang->line('MC014'));
                   return FALSE;
               }
       }

       
       

}

?>